package cn.lili.modules.order.order.entity.dto;

import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.order.order.entity.dos.PrizeOrder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

@Data
public class PrizeOrderDTO extends PageVO {

    /**
     * 昵称
     */
    private String nickName;
    /**
     * 奖品名称
     */
    private String prizeName;

    /**
     * 状态,1待领取，2已领取
     */
    private Integer prizeStatus;

    /**
     * 状态,1实物，2虚拟
     */
    private Integer prizeType;

    /**
     * 是否发货，1未发货，2已发货
     */
    private Integer deliveryOrNot;

    /**
     * 开始时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;
    /**
     * 结束时间
     */
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;

    /**
     * account
     */
    private String memberName;

    public LambdaQueryWrapper<PrizeOrder> toQueryWrapper() {
        LambdaQueryWrapper<PrizeOrder> query = new LambdaQueryWrapper<>();
        //用户昵称
        query.like(StringUtils.isNotEmpty(nickName), PrizeOrder::getNickName, nickName);
        //用户账号
        query.eq(StringUtils.isNotEmpty(memberName), PrizeOrder::getMemberName, memberName);
        //奖品名称
        query.like(StringUtils.isNotEmpty(prizeName), PrizeOrder::getPrizeName, prizeName);
        //奖品类型
        query.eq(null != prizeType, PrizeOrder::getPrizeType, prizeType);
        //领取状态
        query.eq(null != prizeStatus, PrizeOrder::getPrizeStatus, prizeStatus);
        //是否发货: 1未发货，2已发货
        if (deliveryOrNot != null) {
            if (deliveryOrNot.equals(1)) {
                query.and(q -> q.isNull(PrizeOrder::getExpressNo).or().eq(PrizeOrder::getExpressNo, ""));
            }
            if (deliveryOrNot.equals(2)) {
                query.isNotNull(PrizeOrder::getExpressNo);
                query.ne(PrizeOrder::getExpressNo, "");
            }
        }
        //创建时间
        if (startTime != null && null != endTime) {
            query.ge(true, PrizeOrder::getPayTime, startTime).le(true, PrizeOrder::getPayTime, endTime);
        }
        //只查询已支付的
        query.eq(PrizeOrder::getPayStatus, 2);
        //排序
        query.orderByDesc(PrizeOrder::getPayTime).orderByDesc(PrizeOrder::getId);
        return query;
    }


}
